package comWorld.blogCatalog;

import java.util.List;

import com.gjs.tools.JDBC.JDBCTools;

public class BlogCatalogManage {
	
	public static List<BlogCatalog> getBlogCatalogs(int userId) {
		String sql = "select user_id userId, catalog_id catalogId, name, sort_order sortOrder from blog_catalog where user_id = ? ORDER BY sort_order";
		List<BlogCatalog> catalogs = JDBCTools.getObjects(BlogCatalog.class, sql, userId);
		return catalogs;
	}
	
	public static boolean addNewBlogCatalogs(BlogCatalog... args) {
		String sql = "insert into blog_catalog values(?, catalog_id_sq.nextval, ?, ?)";
		for (int i = 0; i < args.length; i++) {
			JDBCTools.insert(sql, args[i].getUserId(), args[i].getName(), args[i].getSortOrder());
		}
		return true;
	}
	
	public static boolean updateBlogCatalog(BlogCatalog catalog) {
		String sql = "update blog_catalog set name = ?, sort_order = ? where catalog_id = ?";
		return JDBCTools.update(sql, catalog.getName(), catalog.getSortOrder(), catalog.getCatalogId()) > 0;
	}
	
	public static boolean deleteBlogCatalog(int catalogId) {
		String sql = "DELETE FROM blog_catalog WHERE catalog_id = ?";
		JDBCTools.update(sql, catalogId);
		sql = "DELETE FROM blog_info WHERE catalog_id = ?";
		JDBCTools.update(sql, catalogId);
		return true;
	}
	
	public static BlogCatalog getBlogCatalog(int catalogId) {
		String sql = "select user_id userId, catalog_id catalogId, name, sort_order sortOrder from blog_catalog where catalog_id = ?";
		return JDBCTools.getObject(BlogCatalog.class, sql, catalogId);
	}
	
	public static boolean queryHasBlogCatalog(int catalogId) {
		String sql = "SELECT * FROM blog_catalog WHERE catalog_id = ?";
		return JDBCTools.queryHasData(sql, catalogId) > 0;
	}
}
